﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EnvDTE;
using SPVisualDev.VSAddin.Dialogs.PropertySheets;

namespace SPVisualDev.VSAddin.Dialogs
{
    public partial class DialogEventClassSelector : Form
    {
        private Project m_project;
        private List<CodeClass> m_classes_init;

        public DialogEventClassSelector(Project project)
        {
            InitializeComponent();

            m_project = project;

            this.lstClassesList.DisplayMember = "FullName";
        }

        public string StartupCodeFolder
        {
            get;
            set;
        }

        public string DefaultNamespace
        {
            get;
            set;
        }

        public string SuggestedName
        {
            get;
            set;
        }

        public List<string> BaseClassesConstraint
        {
            get;
            set;
        }

        public List<CodeClass> CodeClassesList
        {
            get
            {
                m_classes_init.Clear();
                m_classes_init.AddRange(this.lstClassesList.Items.OfType<CodeClass>());

                return m_classes_init;
            }
            set
            {
                m_classes_init = value;

                this.lstClassesList.Items.Clear();

                foreach (CodeClass cls in m_classes_init)
                {
                    this.lstClassesList.Items.Add(cls);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                using (DialogCodeFileSelector dialog = new DialogCodeFileSelector(m_project, this.DefaultNamespace))
                {
                    dialog.StartupFolder = this.StartupCodeFolder;
                    dialog.SuggestedName = this.SuggestedName;
                    dialog.Text = "Choose an event receiver class";

                    DialogResult result = dialog.ShowDialog();
                    if (result != DialogResult.OK)
                        return;

                    if (dialog.SelectedCodeClass == null)
                    {
                        throw new Exception("No class selected.");
                    }

                    if (this.BaseClassesConstraint != null)
                    {
                        int c = 0;
                        foreach (string baseClass in this.BaseClassesConstraint)
                        {
                            if (Common.HasBaseCodeClass(dialog.SelectedCodeClass, baseClass))
                            {
                                c++;
                            }
                        }

                        if (c == 0)
                        {
                            throw new Exception("Selected class must inherit from SPItemEventReceiver or SPListEventReceiver.");
                        }
                    }

                    if (!this.lstClassesList.Items.Contains(dialog.SelectedCodeClass))
                    {
                        this.lstClassesList.Items.Add(dialog.SelectedCodeClass);
                    }
                }
            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (this.lstClassesList.SelectedItem != null)
            {
                this.lstClassesList.Items.Remove(this.lstClassesList.SelectedItem);
            }
        }
    }
}
